package com.dao;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class UsersJdbc {
    Connection conn = null;
    Statement stem;
    ResultSet rs ;
    public  void  lianjie (){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //数据库用户名密码要写自己的
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc", "root", "123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc", "root", "123456");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    public ResultSet show(){
        this.lianjie();
        // 向数据库发送sql 语句的
        try {
            stem = conn.createStatement();
            rs = stem.executeQuery(" select  * from  users ");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  rs;
    }

    public  void  close(){
        try {
            if(conn!=null){
                conn.close();
            }

            if(stem!=null){
                stem.close();
            }

            if(rs!=null){
                rs.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void add(String user_name,String user_password,String user_email,String user_ctime) {
        this.lianjie();
        try {
            user_ctime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
            stem = conn.createStatement();
            System.out.println(user_ctime);
            stem.executeUpdate("insert into users values (null,'"+user_name+"' , '"+user_password+"', '"+user_email+"' , '"+user_ctime+"' ) ");
        }catch (SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.close();
    }

    public void delete(String id){
        this.lianjie();
        try {
            stem = conn.createStatement();
            stem.executeUpdate("delete from users where user_id = '"+id+"'") ;
        }catch (SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.close();
    }

    public void update(String user_id,String user_name,String user_password,String user_email,String user_ctime) {
        this.lianjie();
        try {
            stem = conn.createStatement();
            stem.executeUpdate("update users set user_name='"+user_name+"' , user_password='"+user_password+"' , user_email='"+user_email+"' , user_ctime='"+user_ctime+"'  where user_id='"+user_id+"' ");
        }catch (SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.close();
    }
}
